package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyyCwMdpd;
import cn.stylefeng.guns.sys.modular.system.entity.BtyyCwSyfs;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyyCwMdpdMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyyCwSyfsMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.CwbbParam;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Service
public class BtyMdpdService extends ServiceImpl<BtyyCwMdpdMapper, BtyyCwMdpd> {
    @Resource
    private BtyyCwMdpdMapper mdpdMapper;

    /**
     * 获取辅助核算余额列表
     */
    public Page<Map<String, Object>> list(CwbbParam cwbbParam) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page,cwbbParam);
    }
    /**
     * 获取门店盘点列表（不分页）
     */
    public List<BtyyCwMdpd> listExportMdpd(CwbbParam cwbbParam) {

        return this.baseMapper.listExportMdpd(cwbbParam);
    }
    /**
     * 导入门店盘点excel
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchImportMdpd(String fileName, MultipartFile file) throws Exception {
        boolean notNull = false;
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        //获取业务日期  门店盘点MDPD00003372-半天妖邹平圣豪店-邹平圣豪店仓-2020-10-31.xls
        String mds[]=fileName.split("-");
        String dm=mds[1];
        String ywrq=mds[3]+"-"+mds[4];

        List<BtyyCwMdpd>list=baseMapper.selectList(new QueryWrapper<BtyyCwMdpd>().eq("dm",dm)
                .eq("ywrq",ywrq));
        if(list.size()>0){
            baseMapper.delete(new QueryWrapper<BtyyCwMdpd>().eq("dm",dm)
                    .eq("ywrq",ywrq));
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        //excel中多个sheet表，获取所有sheet表的数量
        int sheets = wb.getNumberOfSheets();
        for (int i = 0; i < sheets; i++) {
            Sheet sheet = wb.getSheetAt(i);
//            //获取sheetName截取门店名称
//            String sheetName=sheet.getSheetName();
//            String md[]=sheetName.split("-");
//            String dm=md[1];
            if (sheet != null) {
                notNull = true;
            }

            int lastRowNum = sheet.getLastRowNum();
            for (int r = 1; r <= lastRowNum; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                for(int m=0;m<28;m++){
                    Cell cell = row.getCell(m);
                    if (ToolUtil.isNotEmpty(cell)) {
                        cell.setCellType(CellType.STRING);
                    }
                }
                String pxlb = row.getCell(0) == null ? "" : row.getCell(0).getStringCellValue();
                String pxbh = row.getCell(1) == null ? "" : row.getCell(1).getStringCellValue();
                String pxmc = row.getCell(2) == null ? "" : row.getCell(2).getStringCellValue();
                String bzxx = row.getCell(3) == null ? "" : row.getCell(3).getStringCellValue();
                String pxgg = row.getCell(4) == null ? "" : row.getCell(4).getStringCellValue();
                String zcsl = row.getCell(5) == null ? "" : row.getCell(5).getStringCellValue();
                String dw = row.getCell(6) == null ? "" : row.getCell(6).getStringCellValue();
                String zcsl2 = row.getCell(7) == null ? "" : row.getCell(7).getStringCellValue();
                String dw2 = row.getCell(8) == null ? "" : row.getCell(8).getStringCellValue();
                String spsl = row.getCell(9) == null ? "" : row.getCell(9).getStringCellValue();
                String dw3 = row.getCell(10) == null ? "" : row.getCell(10).getStringCellValue();
                String spsl2 = row.getCell(11) == null ? "" : row.getCell(11).getStringCellValue();
                String dw4 = row.getCell(12) == null ? "" : row.getCell(12).getStringCellValue();
                String zcje = row.getCell(13) == null ? "" : row.getCell(13).getStringCellValue();
                String hszcje = row.getCell(14) == null ? "" : row.getCell(14).getStringCellValue();
                String spje = row.getCell(15) == null ? "" : row.getCell(15).getStringCellValue();
                String hsspje = row.getCell(16) == null ? "" : row.getCell(16).getStringCellValue();
                String cysl = row.getCell(17) == null ? "" : row.getCell(17).getStringCellValue();
                String dw5 = row.getCell(18) == null ? "" : row.getCell(18).getStringCellValue();
                String ykje = row.getCell(19) == null ? "" : row.getCell(19).getStringCellValue();
                String hspkje = row.getCell(20) == null ? "" : row.getCell(20).getStringCellValue();
                String fzdw = row.getCell(21) == null ? "" : row.getCell(21).getStringCellValue();
                String fzdwzcsl = row.getCell(22) == null ? "" : row.getCell(22).getStringCellValue();
                String fzdwspsl = row.getCell(23) == null ? "" : row.getCell(23).getStringCellValue();
                String fzdwcysl = row.getCell(24) == null ? "" : row.getCell(24).getStringCellValue();
                String kwh = row.getCell(25) == null ? "" : row.getCell(25).getStringCellValue();
                String pch = row.getCell(26) == null ? "" : row.getCell(26).getStringCellValue();
                String bz = row.getCell(27) == null ? "" : row.getCell(27).getStringCellValue();
                BtyyCwMdpd mdpd=new BtyyCwMdpd();
                mdpd.setDm(dm);
                mdpd.setYwrq(ywrq);
                mdpd.setPxlb(pxlb);
                mdpd.setPxbh(pxbh);
                mdpd.setPxmc(pxmc);
                mdpd.setBzxx(bzxx);
                mdpd.setPxgg(pxgg);
                mdpd.setZcsl(zcsl);
                mdpd.setDw(dw);
                mdpd.setZcslTwo(zcsl2);
                mdpd.setDwTwo(dw2);
                mdpd.setSpsl(spsl);
                mdpd.setDwThree(dw3);
                mdpd.setSpslTwo(spsl2);
                mdpd.setDwFour(dw4);
                mdpd.setZcje(zcje);
                mdpd.setHszcje(hszcje);
                mdpd.setSpje(spje);
                mdpd.setHsspje(hsspje);
                mdpd.setCysj(cysl);
                mdpd.setDwFive(dw5);
                mdpd.setYkje(ykje);
                mdpd.setHspkje(hspkje);
                mdpd.setFzdw(fzdw);
                mdpd.setFzdwzcsl(fzdwzcsl);
                mdpd.setFzdwspsl(fzdwspsl);
                mdpd.setFzdwcysl(fzdwcysl);
                mdpd.setKwh(kwh);
                mdpd.setPch(pch);
                mdpd.setBz(bz);
                mdpdMapper.insert(mdpd);
            }
        }
        return notNull;
    }

}
